from flask_login import login_required
from flask import request

from ..base import base
from ..service.ProductService import *

from exts import success, page_success


@base.route('/api/product/add', methods=['POST'])
@login_required
def product_add_api():
    '''
    添加产品
    :return:
    '''
    product = request.get_json()
    product_add(product)
    return success(product)


@base.route('/api/product/update', methods=['POST'])
@login_required
def product_update_api():
    '''
    更新产品
    :return:
    '''
    product = request.get_json()
    product_update(product)
    return success(product)


@base.route('/api/product/delete', methods=['POST'])
@login_required
def product_delete_api():
    '''
    删除解决信息
    :return:
    '''
    product = request.get_json()
    product_delete(product['id'])
    return success(product['id'])


@base.route('/api/product/detail', methods=['POST'])
@login_required
def product_detail_api():
    '''
    查询解决方案信息
    :return:
    '''
    solution = request.get_json()
    return success(product_get_by_id(solution['id']))


@base.route('/api/product/page', methods=['POST'])
@login_required
def product_page_api():
    '''
    分页查询商品
    :return:
    '''
    product = request.get_json()
    page = product_page(product['pageNum'], product['pageSize'])
    return page_success(page.total, page.items)


# =================================pc产品接口=======================================


@base.route('/api/product/tree', methods=['GET'])
def product_tree_api():
    '''
    树形接口
    :return:
    '''
    tree = product_tree()
    return success(tree)
